//
//  THChangeAddPriceViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/21.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THChangeAddPriceViewController.h"

@interface THChangeAddPriceViewController ()
@property(nonatomic, strong) UITextField                     *priceTextFiled;/**<  */
@end

@implementation THChangeAddPriceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.name;
    [self stoneobs_ui_configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    header.backgroundColor = UIColor.whiteColor;
    __weak typeof(self) weakSelf =  self;
    CGFloat top = 0;
    ({
        UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH, 10)];
        stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
        [header addSubview:stoneobs_ui_line];
        top = stoneobs_ui_line.bottom + 20;
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, UISCREEN_WIDTH, 20)
                                                                text:@"价格"
                                                           textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                                font:13
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [header addSubview:stoneobs_ui_label];
        top = stoneobs_ui_label.bottom;
        
        UITextField * textFiled = [UITextField.alloc initWithFrame:CGRectMake(15, top, UISCREEN_WIDTH - 30, 40)];
        textFiled.width = UISCREEN_WIDTH - 30;
        textFiled.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        textFiled.placeholder = @"请输入价格";
        textFiled.top = top;
        textFiled.left = 15;
        textFiled.height = 40;
        textFiled.textAlignment = NSTextAlignmentLeft;
        [textFiled stoneobs_fun_st_showBottomLine];
        [header addSubview:textFiled];
        self.priceTextFiled = textFiled;
        top = textFiled.bottom + 15;
    });
    
    header.height = top;
    self.tableView.tableHeaderView = header;
    

    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 100)];
    
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(20, 50, UISCREEN_WIDTH - 40, 44)
                                                  title:@"提交"
                                             titleColor:UIColor.whiteColor
                                              titleFont:15
                                           cornerRadius:22
                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.centerX = self.tableView.width / 2;
    [footer addSubview:button];
    [button setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedAddButton];
    }];
    button.titleLabel.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
    self.tableView.tableFooterView = footer;
}
- (void)onSlectedAddButton{
    if (!self.priceTextFiled.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入价格"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:self.priceTextFiled.text forKey:@"price"];
    if (self.model.m_id.length) {
        [stoneobs_net_param_dic setObject:self.model.m_id forKey:@"id"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/nursery/editPrice"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });

        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
@end
